package com.atguigu.eduservice.controller.front;

import com.atguigu.commonutils.JwtUtils;
import com.atguigu.commonutils.Response;
import com.atguigu.commonutils.ordervo.CourseWebOrder;
import com.atguigu.eduservice.client.OrdersClient;
import com.atguigu.eduservice.entity.chapter.ChapterVo;
import com.atguigu.eduservice.entity.frontvo.CourseFrontVo;
import com.atguigu.eduservice.entity.frontvo.CourseWebVo;
import com.atguigu.eduservice.entity.vo.CourseQuery;
import com.atguigu.eduservice.service.EduChapterService;
import com.atguigu.eduservice.service.EduCourseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.web.HttpRequestHandler;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @Author zhaolie
 * @Date 2020/12/19 2:26 下午
 * @Version 1.0
 * @Description
 */

@RestController
@RequestMapping("/eduservice/coursefront")
@Api(description = "课程前台")
public class CourseFrontController {

    @Resource
    private EduCourseService courseService;

    @Resource
    private EduChapterService chapterService;

    @Resource
    private OrdersClient ordersClient;

    @ApiOperation("带条件和分页查询课程")
    @PostMapping("/{current}/{limit}")
    private Response getCourseList(@RequestBody(required = false) CourseFrontVo courseFrontVo,
                                   @PathVariable("current") Long current,
                                   @PathVariable("limit") Long limit) {
        Map<String, Object> courseList = courseService.getCourseList(courseFrontVo, current, limit);
        return Response.SUCCESS().data(courseList);
    }

    @ApiOperation("课程详情页")
    @GetMapping("/{courseId}")
    public Response getCourseInfo(@PathVariable String courseId, HttpServletRequest request) {
        CourseWebVo courseInfo = courseService.getBaseCourseInfo(courseId);
        List<ChapterVo> chapterVideoCourse = chapterService.getChapterVideoCourseId(courseId);

        String memberId = JwtUtils.getMemberIdByJwtToken(request);
        boolean isBuy = ordersClient.isBuyCourse(courseId, memberId);
        return Response.SUCCESS().data("courseInfo", courseInfo).data("chapter", chapterVideoCourse).data("isBuy", isBuy);
    }

    @GetMapping("/getCourseInfoOrder/{id}")
    public CourseWebOrder getCourseInfoOrder(@PathVariable("id") String id) {
        CourseWebVo courseInfo = courseService.getBaseCourseInfo(id);
        CourseWebOrder courseWebOrder = new CourseWebOrder();
        BeanUtils.copyProperties(courseInfo, courseWebOrder);
        return courseWebOrder;
    }
}
